package com.onadake.domain;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;

import com.onadake.utils.ValidationUtils;

/**
 * Relation object. This is always one way relation between 2 entities.
 * 
 * @author radek.hecl
 *
 */
public class Relation {

    /**
     * Builder object.
     *
     */
    public static class Builder {

        /**
         * Code of the subject in relation.
         */
        private String subjectCode;
        
        /**
         * Type of the subject in relation.
         */
        private EntityType subjectType;
        
        /**
         * Name of the subject in relation.
         */
        private String subjectName;
        
        /**
         * Code of the object in relation.
         */
        private String objectCode;
        
        /**
         * Type of the object in relation.
         */
        private EntityType objectType;
        
        /**
         * Name of the object in relation.
         */
        private String objectName;
        
        /**
         * Type of the relation.
         */
        private RelationType type;

        /**
         * Sets subject entity code. 
         * 
         * @param subjectCode subject entity code
         * @return this instance
         */
        public Builder setSubjectCode(String subjectCode) {
            this.subjectCode = subjectCode;
            return this;
        }

        /**
         * Sets subject type. 
         * 
         * @param subjectType subject type
         * @return this instance
         */
        public Builder setSubjectType(EntityType subjectType) {
            this.subjectType = subjectType;
            return this;
        }

        /**
         * Sets subject name. 
         * 
         * @param subjectName subject name
         * @return this instance
         */
        public Builder setSubjectName(String subjectName) {
            this.subjectName = subjectName;
            return this;
        }

        /**
         * Sets object entity code. 
         * 
         * @param objectCode object entity code
         * @return this instance
         */
        public Builder setObjectCode(String objectCode) {
            this.objectCode = objectCode;
            return this;
        }

        /**
         * Sets object type. 
         * 
         * @param objectType object type
         * @return this instance
         */
        public Builder setObjectType(EntityType objectType) {
            this.objectType = objectType;
            return this;
        }

        /**
         * Sets object name. 
         * 
         * @param objectName object name
         * @return this instance
         */
        public Builder setObjectName(String objectName) {
            this.objectName = objectName;
            return this;
        }

        /**
         * Sets relation type.
         * 
         * @param type relation type 
         * @return this instance
         */
        public Builder setType(RelationType type) {
            this.type = type;
            return this;
        }

        /**
         * Builds the result object.
         * 
         * @return created object
         */
        public Relation build() {
            return new Relation(this);
        }
        
    }
    
    /**
     * Code of the subject in relation.
     */
    private String subjectCode;
    
    /**
     * Type of the subject in relation.
     */
    private EntityType subjectType;
    
    /**
     * Name of the subject in relation.
     */
    private String subjectName;
    
    /**
     * Code of the object in relation.
     */
    private String objectCode;
    
    /**
     * Type of the object in relation.
     */
    private EntityType objectType;
    
    /**
     * Name of the object in relation.
     */
    private String objectName;
    
    /**
     * Type of the relation.
     */
    private RelationType type;

    /**
     * Creates new instance.
     * 
     * @param builder builder object
     */
    public Relation(Builder builder) {
        subjectCode = builder.subjectCode;
        subjectType = builder.subjectType;
        subjectName = builder.subjectName;
        objectCode = builder.objectCode;
        objectType = builder.objectType;
        objectName = builder.objectName;
        type = builder.type;
        guardInvariants();
    }
    
    /**
     * Guards this object to be consistent. Throws exception if this is not the case.
     */
    private void guardInvariants() {
        ValidationUtils.guardMatch(subjectCode, "[A-Z]{2}[0-9]{16}", "subjectCode must match [A-Z]{2}[0-9]{16}");
        ValidationUtils.guardNotNull(subjectType, "subjectType cannot be null");
        ValidationUtils.guardNotNull(subjectName, "subjectName cannot be null");
        ValidationUtils.guardMatch(objectCode, "[A-Z]{2}[0-9]{16}", "objectCode must match [A-Z]{2}[0-9]{16}");
        ValidationUtils.guardNotNull(objectType, "objectType cannot be null");
        ValidationUtils.guardNotNull(objectName, "objectName cannot be null");
        ValidationUtils.guardNotNull(type, "type cannot be null");
    }    
    
    /**
     * Returns the subject entity code.
     * 
     * @return subject entity code
     */
    public String getSubjectCode() {
        return subjectCode;
    }

    /**
     * Returns the subject type.
     * 
     * @return subject type
     */
    public EntityType getSubjectType() {
        return subjectType;
    }

    /**
     * Returns the subject name.
     * 
     * @return subject name
     */
    public String getSubjectName() {
        return subjectName;
    }

    /**
     * Returns the object entity code.
     * 
     * @return object entity code
     */
    public String getObjectCode() {
        return objectCode;
    }

    /**
     * Returns the object type.
     * 
     * @return object type
     */
    public EntityType getObjectType() {
        return objectType;
    }

    /**
     * Returns the object name.
     * 
     * @return object name
     */
    public String getObjectName() {
        return objectName;
    }

    /**
     * Returns the relation type.
     * 
     * @return relation type
     */
    public RelationType getType() {
        return type;
    }

    @Override
    public int hashCode() {
        return HashCodeBuilder.reflectionHashCode(this);
    }

    @Override
    public boolean equals(Object obj) {
        return EqualsBuilder.reflectionEquals(this, obj);
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }

}
